/**
 * Created by Administrator on 2016/2/15.
 */
$(function () {
    function init() {
        if (config.uploadData.options) {

        }
    }

    init();

    var tpl_single_option = _.template($("#tpl_single_option").html());
    var tpl_singleOption_option = _.template($("#tpl_singleOption_option").html());
    $("#options").delegate("div[name=option]", "click", "", function () {

        var $this = $(this);
        if ($this.attr("baseOption") == "true") {
            //默认选项无法操作
            return;
        }
        if ($this.hasClass("fj-chosed")) {
            $this.removeClass("fj-chosed fj_error").addClass("fj-not-chosed");
        } else {
            $this.removeClass("fj-not-chosed fj_error").addClass("fj-chosed");
        }
    });


    //点击添加选项
    $("#addOtherOption").click(function () {
        var tplOption = _.template($("#tpl_other_option").html());
        $(this).before(tplOption({optionName: ""}));
    });

    //点击添加单选模块
    $("#addSingleOption").click(function () {
        $("#addSingleOption").before(tpl_single_option());
    });

    //点击添加单个选项
    $("#singleOptionList").delegate("[name=addSingleOption]", "click", "", function () {
        var $this = $(this);
        var optionNum = Number($this.attr("optionNum")) + 1;
        if (optionNum > constant.maxSingleOptionNum) {
            CK.alert("最多增加十个单选选项");
            return;
        }
        $this.parents("div.form-group").before(tpl_singleOption_option({optionNum: optionNum}));
        $this.attr("optionNum", optionNum);
    });

    //输入后去除错误标记
    $("#uploadContainer").delegate("[forinput]", "propertychange input", function () {
        $(this).parents("div.has-error").removeClass("has-error");
    });

    //删除按钮点击事件
    $("#otherOptions").delegate("[name=deleteOtherOption]", 'click', function () {
        var $this = $(this);
        $this.parents("div[name=otherOptionDiv]").remove();
    });

    $("#singleOptionList").delegate("[name=deleteSingleOption]", 'click', function () {
        var $this = $(this);
        $this.parents("div[name=singleOption]").remove();
    });


    //上一步点击事件
    $("#optionSetToDetail").click(function () {
        getData();
        CK.load("html/upLoadActivity/detailInfo", $("#uploadContainer"));
    });
    //完成点击事件
    $("#optionSetComplete").click(function () {
        if (getData()) {
            submitData();
            CK.navigator();
        }

    });
    //预览点击事件
    $("#optionSetComplete").click(function () {
        //待作
    });

    //缓存页面数据
    function getData() {
        //debugger;
        var flag = true;
        var options = [], index = 0;
        //保存k-v项
        $("div.fj-chosed[name=option]").each(function () {
            var optionValue = $(this).find("p").html();
            if (!optionValue) {
                $(this).addClass("has-error");
                CK.alert("选项值不能为空");
                flag = false;
            }
            var data = {
                index: index,
                opsType: 2,
                opsStr: optionValue
            };
            options.push(data);
            index++;
        });


        //保存单文本内容
        $("[name=otherOptionDiv]").each(function () {
            var optionValue = $(this).find("[name=otherOption]").val();
            if (!optionValue) {
                $(this).addClass("has-error");
                CK.alert("选项值不能为空");
                flag = false;
            }
            var data = {
                index: index,
                opsType: 2,
                opsStr: optionValue
            };
            options.push(data);
            index++;
        });

        //自定义单选问题
        $("form[name=singleOption]").each(function () {
            var $this = $(this);
            var optionValue = $this.find("input[name=optionName]").val();
            if (!optionValue) {
                $(this).find("[name=optionName]").parent("div").addClass("has-error");
                CK.alert("选项名称不能为空");
                flag = false;
            }
            $this.find("input[name=optionSelect]").each(function () {
                var optionSelectValue = $(this).val();
                if (!optionSelectValue) {
                    $(this).parents("div.form-group").addClass("has-error");
                    CK.alert("选项值不能为空");
                    flag = false;
                }
                optionValue += "#" + optionSelectValue;
            });
            var data = {
                index: index,
                opsType: 2,
                opsStr: optionValue
            };
            options.push(data);
            index++;
        });
        if (flag) {
            config.uploadData.options = options;
        }
        return flag;
    }

    //上传数据
    function submitData() {
        var dataSub =
        {
            userId: config.fj_user_id,
            orgId: config.fj_org_id,
            atvSubj: config.uploadData.theme,
            atvStartTime: config.uploadData.dateStart + " " + config.uploadData.timeStart,
            atvEndTime: config.uploadData.dateEnd + " " + config.uploadData.timeEnd,
            signUpEndTime: config.uploadData.deadlineDate + " " + config.uploadData.deadlineTime,
            atvDesc: config.uploadData.atvDesc,
            atvType: config.uploadData.type,
            atvCityId: config.uploadData.city,
            atvUniId: config.uploadData.school,
            upperBound: config.uploadData.upbound,
            posterId: config.uploadData.img,
            ops: config.uploadData.options
        };
        $.ajax({
            url: CK.formatUrl(config.service.uploadAct),
            data: dataSub,
            type: "POST",
            async: "true",
            success: function (response) {
                if (code != 0) {
                    CK.alert(response.msg);
                    return;
                }
                //上传成功，跳转
                config.uploadData.atvId = response.data.atvId;
                CK.load("html/upLoadActivity/uploadComplete", $("#uploadContainer"));
            },
        });
    }
});
